﻿using Cysharp.Threading.Tasks;
using UnityEngine;

/// <summary>
/// UI基类
/// </summary>
public abstract class UIPanelBase : UIBehaviour
{
    [SerializeField]
    public UIPanelType type = UIPanelType.Normal;
    [SerializeField]
    public UIShowAnimaMode showAnimaMode = UIShowAnimaMode.Default;
    [SerializeField]
    public RectTransform animaWindow;
    [SerializeField]
    public float animaDuration = 0.2f;

    /// <summary>
    /// 用于界面暂停时阻挡界面
    /// </summary>
    [HideInInspector]
    public GameObject mask;

    private string m_UIName;
    public string uiName => m_UIName ?? (m_UIName = GetType().Name);

    public bool active => UIManager.FindActive(uiName) != null;


    #region 生命周期函数，由 UIManager 统一调用

    /// <summary>
    /// 初始化
    /// </summary>
    public virtual void OnInit() { }

    /// <summary>
    /// 显示时
    /// </summary>
    public virtual void OnShow() { }

    /// <summary>
    /// 隐藏时
    /// </summary>
    public virtual void OnHide() { }

    /// <summary>
    /// 暂停时，界面停止更新显示停止接收消息
    /// </summary>
    public virtual void OnPause() { }

    /// <summary>
    /// 从界面暂停中恢复时
    /// </summary>
    public virtual void OnResume() { }

    /// <summary>
    /// 成为交点时
    /// </summary>
    public virtual void OnFocus() { }

    /// <summary>
    /// 卸载时
    /// </summary>
    public virtual void OnRelease() { }

    #endregion 生命周期函数，由 UIManager 统一调用

    #region UI显示动画回调

    /// <summary>
    /// 显示动画开始时，用于界面表现逻辑处理
    /// </summary>
    public virtual UniTask OnShowingStart() => UniTask.CompletedTask;

    /// <summary>
    /// 显示动画结束时，用于界面表现逻辑处理
    /// </summary>
    public virtual UniTask OnShowingEnd() => UniTask.CompletedTask;

    /// <summary>
    /// 隐藏动画开始时，用于界面表现逻辑处理
    /// </summary>
    public virtual UniTask OnHidingStart() => UniTask.CompletedTask;

    /// <summary>
    /// 隐藏动画结束时，用于界面表现逻辑处理
    /// </summary>
    public virtual UniTask OnHidingEnd() => UniTask.CompletedTask;

    #endregion UI显示动画回调

    #region 显示隐藏自调用封装

    /// <summary>
    /// 显示
    /// </summary>
    public void Show()
    {
        UIManager.Show(uiName);
    }

    /// <summary>
    /// 隐藏
    /// </summary>
    public void Hide()
    {
        UIManager.Hide(uiName);
    }

    /// <summary>
    /// 切换
    /// </summary>
    public bool Switch()
    {
        return UIManager.Switch(uiName);
    }

    #endregion 显示隐藏自调用封装
}

/// <summary>
/// 面板类型
/// </summary>
public enum UIPanelType
{
    /// <summary>
    /// 常驻/固定
    /// </summary>
    Fixed,
    /// <summary>
    /// 普通
    /// </summary>
    Normal,
    /// <summary>
    /// 弹窗
    /// </summary>
    Popup,
    /// <summary>
    /// 顶层，用于Hint、Toast、Loading等
    /// </summary>
    Top,
    /// <summary>
    /// 格挡，用于阻挡所有输入
    /// </summary>
    Block
}

/// <summary>
/// 显示动画模式
/// </summary>
public enum UIShowAnimaMode
{
    /// <summary>
    /// 默认，无动画
    /// </summary>
    Default,
    /// <summary>
    /// 淡入淡出
    /// </summary>
    Fade,
    /// <summary>
    /// 弹出
    /// </summary>
    Popup
}